package main

import (
    "facecat"
    "io/ioutil"  
    "net/http"
    "fmt"
    "strings"
    "time"
)

var gPaint = facecat.CreatePaint()
var latestDataStr = ""
var gridStocks = facecat.CreateView("grid")
var findMyCharts []*facecat.FCView
var charts []*facecat.FCView

/*
*绘制买卖档
*view 视图
*paint 绘图对象
*clipRect 区域
*/
func drawLatestDiv(view *facecat.FCView, paint *facecat.FCPaint, clipRect facecat.FCRect){
    avgHeight := 20.0
	drawFont := "Default,14"
	dTop := 30.0
	paint.DrawLine(view.BorderColor, 1, 0, 0, dTop, view.Size.Cx, dTop)
	dataStrs := strings.Split(latestDataStr, ",")
	lastClose := 0.0
	var priceList []float64
	var volList []float64
	var buySellTexts []string
    if len(dataStrs) > 10{
		paint.DrawText(dataStrs[0], "rgb(175,196,228)", "Default,14", 5, 7)
		paint.DrawText(dataStrs[1], "rgb(194,151,18)", "Default,14", 80, 7)
		lastClose = facecat.ConvertToFloat64(dataStrs[8])
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[23]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[22]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[21]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[20]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[19]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[9]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[10]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[11]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[12]))
		priceList = append(priceList, facecat.ConvertToFloat64(dataStrs[13]))

		volList = append(volList, facecat.ConvertToFloat64(dataStrs[28]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[27]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[26]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[25]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[24]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[14]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[15]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[16]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[17]))
		volList = append(volList, facecat.ConvertToFloat64(dataStrs[18]))
	}
    buySellTexts = append(buySellTexts, "卖5")
	buySellTexts = append(buySellTexts, "卖4")
	buySellTexts = append(buySellTexts, "卖3")
	buySellTexts = append(buySellTexts, "卖2")
	buySellTexts = append(buySellTexts, "卖1")
	buySellTexts = append(buySellTexts, "买1")
	buySellTexts = append(buySellTexts, "买2")
	buySellTexts = append(buySellTexts, "买3")
	buySellTexts = append(buySellTexts, "买4")
	buySellTexts = append(buySellTexts, "买5")
    textColor := "rgb(175,196,228)"
	if view.GetPaint().DefaultUIStyle == "light"{
		textColor = "rgb(0,0,0)"
    }
    maxVol := facecat.MaxValue(volList)
    for i := 0; i < 10; i++ {
        tSize := paint.TextSize(buySellTexts[i], drawFont)
		paint.DrawText(buySellTexts[i], textColor, drawFont, 5, dTop + avgHeight / 2 - tSize.Cy / 2)
        if len(priceList) > 0{
			price := priceList[i]
			upDownColor := "rgb(255,82,82)"
			upDownColor2 := "rgb(50,0,0)"
			if price < lastClose{
				upDownColor = "rgb(46,255,50)"
				upDownColor2 = "rgb(0,50,0)"
			}
			paint.DrawText(facecat.ToFixed(priceList[i], 2), upDownColor, drawFont, 50, dTop + avgHeight / 2 - tSize.Cy / 2)
			if maxVol > 0{
				paint.FillRect(upDownColor2, view.Size.Cx - volList[i] * 80 / maxVol, dTop + 2, view.Size.Cx, dTop + avgHeight - 2)
            }
			volText := facecat.ToFixed(volList[i] / 100, 0)
			volTextSize := paint.TextSize(volText, drawFont)
			paint.DrawText(volText, textColor, drawFont, view.Size.Cx - volTextSize.Cx - 10, dTop + avgHeight / 2 - volTextSize.Cy / 2)
		}
		dTop += avgHeight
    }
    paint.DrawLine(view.BorderColor, 1, 0, 0, dTop, view.Size.Cx, dTop)
	paint.DrawText("现价", "rgb(175,196,228)", drawFont, 5, dTop + 10)
	paint.DrawText("幅度", "rgb(175,196,228)", drawFont, 5, dTop + 35)
	paint.DrawText("总额", "rgb(175,196,228)", drawFont, 5, dTop + 60)
	paint.DrawText("总量", "rgb(175,196,228)", drawFont, 5, dTop + 85)
	paint.DrawText("开盘", "rgb(175,196,228)", drawFont, 110, dTop + 10)
	paint.DrawText("振幅", "rgb(175,196,228)", drawFont, 110, dTop + 35)
	paint.DrawText("最高", "rgb(175,196,228)", drawFont, 110, dTop + 60)
	paint.DrawText("最低", "rgb(175,196,228)", drawFont, 110, dTop + 85)
    if len(dataStrs) > 10{
		close := facecat.ConvertToFloat64(dataStrs[2])
		high := facecat.ConvertToFloat64(dataStrs[3])
		low := facecat.ConvertToFloat64(dataStrs[4])
		openPrice := facecat.ConvertToFloat64(dataStrs[5])
		volume := facecat.ConvertToFloat64(dataStrs[6])
		amount := facecat.ConvertToFloat64(dataStrs[7])
		diff := 0.0
		if lastClose > 0{
			diff = 100 * (close - lastClose) / lastClose
        }
		diff2 := 0.0
		if lastClose > 0{
			diff2 = 100 * (high - lastClose) / lastClose - 100 * (low - lastClose) / lastClose
        }
		paint.DrawText(facecat.ToFixed(close, 2), getPriceColor(close, lastClose), drawFont, 40, dTop + 10)
		paint.DrawText(facecat.ToFixed(diff, 2) + "%", getPriceColor(close, lastClose), drawFont, 40, dTop + 35)
		paint.DrawText(facecat.ToFixed(amount / 10000, 0), textColor, drawFont, 40, dTop + 60)
		paint.DrawText(facecat.ToFixed(volume / 10000, 0), textColor, drawFont, 40, dTop + 85)

		paint.DrawText(facecat.ToFixed(openPrice, 2), getPriceColor(openPrice, lastClose), drawFont, 150, dTop + 10)
		paint.DrawText(facecat.ToFixed(diff2, 2) + "%", getPriceColor(close, lastClose), drawFont, 150, dTop + 35)
		paint.DrawText(facecat.ToFixed(high, 2), getPriceColor(high, lastClose), drawFont, 150, dTop + 60)
		paint.DrawText(facecat.ToFixed(low, 2), getPriceColor(low, lastClose), drawFont, 150, dTop + 85)
	}
}

/*
* 创建单元格
*/
func createGridCell (grid *facecat.FCView) (*facecat.FCGridCell){
    gridCell := facecat.CreateGridCell()
    if grid.GetPaint().DefaultUIStyle == "dark"{
        gridCell.BackColor = "none"
        gridCell.BorderColor = "none"
        gridCell.TextColor = "rgb(175,196,228)"
    }else if grid.GetPaint().DefaultUIStyle == "light"{
        gridCell.BackColor = "none"
        gridCell.BorderColor = "none"
        gridCell.TextColor = "rgb(0,0,0)"
    }
    gridCell.Font = "Default,13"
    return gridCell
}

/*
*获取价格颜色 
*/
func getPriceColor(price float64, comparePrice float64) string{
	if price != 0{
		if price > comparePrice{
			return "rgb(255,82,82)"
		} else if price < comparePrice{
			return "rgb(46,255,50)"
        }
    }
	return "rgb(190,190,235)"
}

/*
*查找同类型视图
*/
func findViewsByType(findType string, views []*facecat.FCView){
    size := len(views)
	for i := 0; i < size; i++ {
		view := views[i]
		if view.ViewType == findType{
			findMyCharts = append(findMyCharts, view)
        }else if len(view.Views) > 0{
			findViewsByType(findType, view.Views)
        }
	}
}

/*
*黑色风格
*/
func toBlackChart(chart *facecat.FCView, index int){
    inChart := chart.GetChart()         
	chart.GetPaint().DefaultUIStyle = "dark"
	chart.BackColor = "rgb(0,0,0)"
	chart.BorderColor = "none"
	chart.TextColor = "rgb(175,196,228)"
	inChart.ScaleColor = "rgb(75,75,75)"
	inChart.CrossTipColor = "rgb(50,50,50)"
	inChart.CrossLineColor = "rgb(100,100,100)"
	inChart.GridColor = "rgb(50,50,50)"
	if index > 0{
		inChart.UpColor = "rgb(186,56,18)"
		inChart.DownColor = "rgb(31,182,177)"
	}else{
		inChart.UpColor = "rgb(255,82,82)"
		inChart.DownColor = "rgb(46,255,50)"
	}
	inChart.BarStyle = "rect2"
	inChart.CandleStyle = "rect2"
	inChart.TrendColor = "rgb(255,255,255)"
	inChart.HScaleTextColor = "rgb(194,151,18)"
	inChart.VScaleTextColor = "rgb(194,151,18)"
}

/*
*请求报价
*/
func queryPrice(codes string){
    url := "http://110.42.188.197:9968/quote?func=price&count=500&codes=" + codes
    resp, err := http.Get(url)
    if err != nil{
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil{
        fmt.Println(err)
    }
    result := string(body)
    strs := strings.Split(result, "\r\n")
    for i := 0; i < len(strs); i++ {
        subStrs := strings.Split(strs[i], ",")
		if len(subStrs) >= 15{
            row := facecat.CreateGridRow()
			gridStocks.GetGrid().Rows = append(gridStocks.GetGrid().Rows, row)
			cell1 := createGridCell(gridStocks)
			cell1.Value = facecat.ConvertIntToStr(i)
			row.Cells = append(row.Cells, cell1)

			cell2 := createGridCell(gridStocks)
			cell2.Value = subStrs[0]
			cell2.TextColor = "rgb(194,151,18)"
			row.Cells = append(row.Cells, cell2)

			cell3 := createGridCell(gridStocks)
			cell3.Value = subStrs[1]
			row.Cells = append(row.Cells, cell3)

			close := facecat.ConvertToFloat64(subStrs[2])
			high := facecat.ConvertToFloat64(subStrs[3])
			low :=  facecat.ConvertToFloat64(subStrs[4])
			lastClose := facecat.ConvertToFloat64(subStrs[8])
			cell4 := createGridCell(gridStocks)
			cell4.Value = facecat.ToFixed(close, 2)
			cell4.TextColor = getPriceColor(close, lastClose)
			row.Cells = append(row.Cells, cell4)
			diff := 0.0
			if lastClose > 0{
				diff = 100 * (close - lastClose) / lastClose
            }
			cell5 := createGridCell(gridStocks)
			cell5.Value = facecat.ToFixed(diff, 2) + "%"
			cell5.TextColor = getPriceColor(diff, 0)
			row.Cells = append(row.Cells, cell5)

			cell6 := createGridCell(gridStocks)
			cell6.Value = facecat.ToFixed(close - lastClose, 2)
			cell6.TextColor = getPriceColor(close, lastClose)
			row.Cells = append(row.Cells, cell6)

			volume := facecat.ConvertToFloat64(subStrs[6])
			amount := facecat.ConvertToFloat64(subStrs[7])
			cell7 := createGridCell(gridStocks)
			cell7.Value = facecat.ToFixed(volume / 100 / 10000, 2) + "万"
			row.Cells = append(row.Cells, cell7)

			cell8 := createGridCell(gridStocks)
			cell8.Value = facecat.ToFixed(amount / 100000000, 2) + "亿"
			row.Cells = append(row.Cells, cell8)

			cell9 := createGridCell(gridStocks)
			cell9.Value = facecat.ToFixed(facecat.ConvertToFloat64(subStrs[12]), 2)
			row.Cells = append(row.Cells, cell9)

			cell10 := createGridCell(gridStocks)
			cell10.Value = facecat.ToFixed(facecat.ConvertToFloat64(subStrs[11]), 2)
			row.Cells = append(row.Cells, cell10)

			diff2 := 0.0
			if lastClose > 0{
				diff2 = 100 * (high - lastClose) / lastClose - 100 * (low - lastClose) / lastClose
            }
			cell11 := createGridCell(gridStocks)
			cell11.Value = facecat.ToFixed(diff2, 2) + "%"
			row.Cells = append(row.Cells, cell11)

			cell12 := createGridCell(gridStocks)
			cell12.Value = facecat.ToFixed(facecat.ConvertToFloat64(subStrs[13]), 2)
			row.Cells = append(row.Cells, cell12)

			marketValue := facecat.ConvertToFloat64(subStrs[9]) * close
			cell13 := createGridCell(gridStocks)
			cell13.Value = facecat.ToFixed(marketValue / 100000000, 2) + "亿"
			row.Cells = append(row.Cells, cell13)

			flowValue := facecat.ConvertToFloat64(subStrs[10]) * close
			cell14 := createGridCell(gridStocks)
			cell14.Value = facecat.ToFixed(flowValue / 100000000, 2) + "亿"
			row.Cells = append(row.Cells, cell14)

			cell15 := createGridCell(gridStocks)
			cell15.Value = ""
			row.Cells = append(row.Cells, cell15)

			upperLimit := facecat.ConvertToFloat64(subStrs[14])
			lowerLimit := facecat.ConvertToFloat64(subStrs[15])
			cell16 := createGridCell(gridStocks)
			cell16.Value = facecat.ToFixed(upperLimit, 2)
			cell16.TextColor = getPriceColor(1, 0)
			row.Cells = append(row.Cells, cell16)

			cell17 := createGridCell(gridStocks)
			cell17.Value = facecat.ToFixed(lowerLimit, 2)
			cell17.TextColor = getPriceColor(0, 1)
			row.Cells = append(row.Cells, cell17)

			cell18 := createGridCell(gridStocks)
			cell18.Value = ""
			row.Cells = append(row.Cells, cell18)

			cell19 := createGridCell(gridStocks)
			cell19.Value = ""
			row.Cells = append(row.Cells, cell19)

			cell20 := createGridCell(gridStocks)
			cell20.Value = ""
			row.Cells = append(row.Cells, cell20)

			cell21 := createGridCell(gridStocks)
			cell21.Value = ""
			row.Cells = append(row.Cells, cell21)

			cell22 := createGridCell(gridStocks)
			cell22.Value = ""
			row.Cells = append(row.Cells, cell22)
        }
    }
    facecat.InvalidateView(gridStocks)
}

/*
*请求历史数据
*/
func queryHistoryData(mychart *facecat.FCView, chart *facecat.FCView, code string){
    strCycle := mychart.ExAttributes["cycle"]
	intCycle := facecat.ConvertToInt(strCycle)
	url := "http://110.42.188.197:9968/quote?func=getkline&code=" + code +  "&cycle=" + strCycle + "&count=500"
	if intCycle == 0{
		url = "http://110.42.188.197:9968/quote?func=getkline&code=" + code +  "&cycle=0&count=240"
    }
    resp, err := http.Get(url)
    if err != nil{
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil{
        fmt.Println(err)
    }
    result := string(body)
    strs := strings.Split(result, "\r\n")
    var dataList[]*facecat.SecurityData
    inChart := chart.GetChart()
    inChart.FirstOpen = 0
	if intCycle == 0{
		fStrs := strings.Split(strs[0], " ")
		if len(fStrs) >= 3{
			inChart.FirstOpen = facecat.ConvertToFloat64(fStrs[2])
		}
	}
    for i := 2; i < len(strs); i++ {
        subStrs := strings.Split(strs[i], ",")
		if len(subStrs) >= 7{
            data := new(facecat.SecurityData)
			if intCycle < 1440{
                dateStr := subStrs[0] + " " + subStrs[1][0:2] + ":" + subStrs[1][2:4] + ":00"
				data.Open = facecat.ConvertToFloat64(subStrs[2])
				data.High = facecat.ConvertToFloat64(subStrs[3])
				data.Low = facecat.ConvertToFloat64(subStrs[4])
				data.Close = facecat.ConvertToFloat64(subStrs[5])
				data.Volume = facecat.ConvertToFloat64(subStrs[6])
                t, _ := time.Parse("2006-01-02 15:04:05", dateStr)
				data.Date = float64(t.Unix()) - 28800
				if intCycle == 0 && (data.Volume > 0 || len(dataList) == 0){
					inChart.LastValidIndex = len(dataList)
					if inChart.FirstOpen == 0{
						inChart.FirstOpen = data.Close
                    }
                }
            }else{
                dateStr := subStrs[0]
				data.Open = facecat.ConvertToFloat64(subStrs[1])
				data.High = facecat.ConvertToFloat64(subStrs[2])
				data.Low = facecat.ConvertToFloat64(subStrs[3])
				data.Close = facecat.ConvertToFloat64(subStrs[4])
				data.Volume = facecat.ConvertToFloat64(subStrs[5])
				t, _ := time.Parse("2006-01-02", dateStr)
				data.Date = float64(t.Unix()) - 28800
			}
			dataList = append(dataList, data)
        }
    }
    if intCycle == 0{
		inChart.AutoFillHScale = true
		inChart.Cycle = "trend"
    }else if intCycle < 1440{
		inChart.Cycle = "minute"
    }else{
		inChart.Cycle = "day"
	}
	inChart.Datas = dataList
	maxVisibleRecord := facecat.GetChartMaxVisibleCount(chart, inChart.HScalePixel, facecat.GetChartWorkAreaWidth(chart))
	inChart.LastVisibleIndex = len(inChart.Datas) - 1
	if maxVisibleRecord > len(inChart.Datas){
		inChart.FirstVisibleIndex = 0
    }else{
		inChart.FirstVisibleIndex = inChart.LastVisibleIndex - maxVisibleRecord + 1
	}
	facecat.ResetChartVisibleRecord(chart)
	facecat.CheckChartLastVisibleIndex(chart)
	facecat.CalcChartIndicator(chart)
	facecat.InvalidateView(chart)
}

/*
*请求最新数据
*/
func queryNewData(code string){
    url := "http://110.42.188.197:9968/quote?func=getnewdata&codes=" + code
    resp, err := http.Get(url)
    if err != nil{
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil{
        fmt.Println(err)
    }
    result := string(body)
    latestDataStr = result
	facecat.Invalidate(gPaint)
}

/*
* 重绘背景的实现方法
* view:视图
* paint:绘图对象
* clipRect:裁剪区域
*/
func OnPaint(view *facecat.FCView, paint *facecat.FCPaint, clipRect facecat.FCRect) {
    if view.ViewType == "latestdiv"{
        drawLatestDiv(view, paint, clipRect)
    }else{
		facecat.OnPaintDefault(view, paint, clipRect)
	}
}

 /*
* 点击单元格
* grid:表格
* row:行
* column:列
* cell:单元格
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
* clicks:点击次数
*/
func OnClickGridCell(grid *facecat.FCView, row *facecat.FCGridRow, column *facecat.FCGridColumn, cell *facecat.FCGridCell, firstTouch bool, firstPoint facecat.FCPoint, secondTouch bool, secondPoint facecat.FCPoint, clicks int){
    code := row.Cells[1].Value
	name := row.Cells[2].Value
	for i := 0; i < len(findMyCharts); i++ {
        myChart := findMyCharts[i]
		chart := charts[i]
		chart.Text = code + " " + name
		queryHistoryData(myChart, chart, code)
		if i >= 2{
			break
        }
    }
	queryNewData(code)
	facecat.Invalidate(grid.GetPaint())
}

/*
* 鼠标点击实现方法
* view: 图层
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
* clicks:点击次数
*/
func OnClick(view *facecat.FCView, firstTouch bool, firstPoint facecat.FCPoint, secondTouch bool, secondPoint facecat.FCPoint, clicks int){
	facecat.OnClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks)
	if strings.Contains(view.ViewName, "cycle,"){
		strs := strings.Split(view.ViewName, ",")
		index := facecat.ConvertToInt(strs[1])
        cycleInt := facecat.ConvertToInt(strs[2])
		findMyCharts[index].ExAttributes["cycle"] = facecat.ConvertIntToStr(cycleInt)
		queryHistoryData(findMyCharts[index], charts[index], strings.Split(charts[index].Text, " ")[0])
	}
}

func WndProc(hwnd facecat.HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
    if msg == facecat.SW_SHOW{
        onLoad()
    }
    return facecat.WndProcDefault(gPaint, hwnd, msg, wParam, lParam)
}

var isInit = true

func onLoad(){
    if isInit{
        isInit = false
        gridStocks = facecat.FindViewByName("gridStocks", gPaint.Views)
        for i := 0; i < len(gridStocks.GetGrid().Columns); i++ {
            gridStocks.GetGrid().Columns[i].CellAlign = "right"
        }
		gridStocks.GetGrid().SelectedRowColor = "rgb(75,75,75)"
		gridStocks.GetGrid().AlternateRowColor = "rgb(25,25,25)"
        queryPrice("all")
        strCode := "600000.SH"
        strName :="浦发银行"
        findViewsByType("mychart", gPaint.Views)
        for i := 0; i < len(findMyCharts); i++ {
            myChart := findMyCharts[i]
            splitDiv := facecat.CreateView("split")
            inSplit := splitDiv.GetSplitDiv() 
            inSplit.LayoutStyle = "toptobottom"
            splitDiv.Size = facecat.CreateSize(400, 400)
            splitDiv.BackColor = "none"
            splitDiv.BorderColor = "none"
            splitDiv.Dock = "fill"
            facecat.AddViewToParent(splitDiv, myChart)

            topDiv := facecat.CreateView("layout")
            inLayout := topDiv.GetLayoutDiv()
            topDiv.BackColor = "none"
            topDiv.BorderColor = "none"
            inLayout.LayoutStyle = "lefttoright"
            topDiv.ShowHScrollBar = false
            facecat.AddViewToParent(topDiv, splitDiv)

            bottomDiv := facecat.CreateView("div")
            bottomDiv.BackColor = "none"
            bottomDiv.BorderColor = "none"
            facecat.AddViewToParent(bottomDiv, splitDiv)

            inSplit.SetFirstView(topDiv)
            inSplit.SetSecondView(bottomDiv)

            splitter := facecat.CreateView("view")
            splitter.SetParent(splitDiv)
            if gPaint.DefaultUIStyle == "dark"{
                splitter.BackColor = "rgb(75,75,75)"
            }else if gPaint.DefaultUIStyle == "light"{
                splitter.BackColor = "rgb(150,150,150)"
            }
			splitter.BorderColor = "none"
            splitter.SetPaint(gPaint)
            splitDiv.Views = append(splitDiv.Views, splitter)
            inSplit.SetSplitter(splitter)
            splitter.Size = facecat.CreateSize(400, 1)
            splitter.Location = facecat.CreatePoint(0, 30)
			if i == 0{
				splitter.Location = facecat.CreatePoint(0, 0)
			}

            chart := facecat.CreateView("chart")
            inChart := chart.GetChart()
            inChart.LeftVScaleWidth = 70
            inChart.RightVScaleWidth = 70
			if i > 0{
				inChart.LeftVScaleWidth = 0
			}
            inChart.VScaleDistance = 35
            inChart.HScalePixel = 11
            inChart.HScaleHeight = 30
            inChart.CandlePaddingTop = 30
            inChart.CandlePaddingBottom = 20
            inChart.VolPaddingTop = 20
            inChart.VolPaddingBottom = 0
            inChart.VScaleDistance = 35
            chart.Dock = "fill"
            chart.Font = "Default,12"
            str1, ok := myChart.ExAttributes["candledivpercent"]
            if ok{
                inChart.CandleDivPercent = facecat.ConvertToFloat64(str1)
            }
            str2, ok2 := myChart.ExAttributes["voldivpercent"]
            if ok2{
                inChart.VolDivPercent = facecat.ConvertToFloat64(str2)
            }
            inChart.IndDivPercent = 0
            chart.Text = strCode + " " + strName
			strCycle := myChart.ExAttributes["cycle"]
			intCycle := facecat.ConvertToInt(strCycle)
			if intCycle == 0{
                chart.Text += " 分时"
            }else if intCycle < 1440{
                chart.Text += " " + facecat.ConvertIntToStr(intCycle) + "分钟"
            }else if intCycle == 1440{
                chart.Text += " 日线"
            }else if intCycle == 10080{
                chart.Text += " 周线"
            }else if intCycle == 43200{
                chart.Text += " 月线"
            }
			inChart.AllowDragChartDiv = true
            charts = append(charts, chart)
            facecat.AddViewToParent(chart, bottomDiv)
            toBlackChart(chart, i)
            queryHistoryData(myChart, chart, strCode)
            var cycles []string
            cycles = append(cycles, "1")
            cycles = append(cycles, "5")
            cycles = append(cycles, "10")
            cycles = append(cycles, "15")
            cycles = append(cycles, "20")
            cycles = append(cycles, "30")
            cycles = append(cycles, "60")
            cycles = append(cycles, "90")
            cycles = append(cycles, "120")
            cycles = append(cycles, "日")
            cycles = append(cycles, "周")
            cycles = append(cycles, "月")
            cycles = append(cycles, "季")
            cycles = append(cycles, "半")
            cycles = append(cycles, "年")
			var cyclesInts []int
            cyclesInts = append(cyclesInts, 1)
            cyclesInts = append(cyclesInts, 5)
            cyclesInts = append(cyclesInts, 10)
            cyclesInts = append(cyclesInts, 15)
            cyclesInts = append(cyclesInts, 20)
            cyclesInts = append(cyclesInts, 30)
            cyclesInts = append(cyclesInts, 60)
            cyclesInts = append(cyclesInts, 90)
            cyclesInts = append(cyclesInts, 120)
            cyclesInts = append(cyclesInts, 1440)
            cyclesInts = append(cyclesInts, 10080)
            cyclesInts = append(cyclesInts, 43200)
            cyclesInts = append(cyclesInts, 129600)
            cyclesInts = append(cyclesInts, 259200)
            cyclesInts = append(cyclesInts, 518400)
            for c := 0; c < len(cycles); c++ {
                cycleButton := facecat.CreateView("button")
                cycleButton.Text = cycles[c]
                cycleButton.Size = facecat.CreateSize(27, 30)
                cycleButton.TextColor = "rgb(200,200,200)"
                cycleButton.BorderColor = "rgb(50,50,50)"
                cycleButton.BackColor = "none"
                facecat.AddViewToParent(cycleButton, topDiv)
				cycleButton.ViewName = "cycle," + facecat.ConvertIntToStr(i) + "," + facecat.ConvertIntToStr(cyclesInts[c])
            }
        }
        queryNewData(strCode)
        facecat.UpdateViewDefault(gPaint.Views)
    }
}

func main() {
    gPaint.OnPaint = OnPaint
    gPaint.OnClickGridCell = OnClickGridCell
	gPaint.OnClick = OnClick
    gPaint.DefaultUIStyle = "dark"
	//gPaint.HighQuanlity()
	//gPaint.ScaleFactorX = 1.33
	//gPaint.ScaleFactorY = 1.33
    facecat.CreateMainWindow(gPaint, "FaceCat-Go", WndProc)
    xmlStr, _ := ioutil.ReadFile("mainframe.xml")
    facecat.RenderFaceCat(gPaint, string(xmlStr))
    facecat.ShowWindow(gPaint)
}